# -*- coding: utf-8 -*-

import os

from webutils_wechat.logger import AppLogger

from utils.common import BASE_PATH


class WebWechatLogger(AppLogger):
    logger_sub_dirs = [
        'api',
        'models',
    ]

    @property
    def logger_dir(self):
        if self._logger_dir is None:
            logger_dir = self.app.config.setdefault('LOGGER_FOLDER', self.app.name)
            if os.environ.get('DOCKER_ENV'):
                logger_dir_top = '/log'
            else:
                logger_dir_top = 'log'
            self._logger_dir = os.path.join(BASE_PATH, logger_dir_top, logger_dir)
        return self._logger_dir

    @property
    def config(self):
        return {
            'formatters': {
                'web_wechat': {
                    'format': '[%(levelname).1s]%(asctime)s [%(name)s]: %(message)s',
                },
            },

            'handlers': {
                'root': {
                    'level': 'DEBUG',
                    'filters': None,
                    'class': 'logging.handlers.TimedRotatingFileHandler',
                    'filename': os.path.join(self.logger_dir, 'site.log'),
                    'backupCount': 10,
                    'when': 'midnight',
                    'formatter': 'web_wechat',
                },
                'api_debug': {
                    'level': 'DEBUG',
                    'filters': None,
                    'class': 'logging.handlers.TimedRotatingFileHandler',
                    'filename': os.path.join(self.logger_dir, 'api', 'debug.log'),
                    'backupCount': 10,
                    'when': 'midnight',
                    'formatter': 'web_wechat',
                },
                'api_warning': {
                    'level': 'WARNING',
                    'filters': None,
                    'class': 'logging.handlers.TimedRotatingFileHandler',
                    'filename': os.path.join(self.logger_dir, 'api', 'warning.log'),
                    'backupCount': 10,
                    'when': 'midnight',
                    'formatter': 'web_wechat',
                },
                'models_debug': {
                    'level': 'DEBUG',
                    'filters': None,
                    'class': 'logging.handlers.TimedRotatingFileHandler',
                    'filename': os.path.join(self.logger_dir, 'models', 'debug.log'),
                    'backupCount': 10,
                    'when': 'midnight',
                    'formatter': 'web_wechat',
                },
                'models_warning': {
                    'level': 'WARNING',
                    'filters': None,
                    'class': 'logging.handlers.TimedRotatingFileHandler',
                    'filename': os.path.join(self.logger_dir, 'models', 'warning.log'),
                    'backupCount': 10,
                    'when': 'midnight',
                    'formatter': 'web_wechat',
                },
            },
            'loggers': {
                '': {
                    'handlers': ['root'],
                    'level': 'DEBUG',
                    'propagate': True,
                },
                'api': {
                    'handlers': ['api_debug', 'api_warning'],
                    'level': 'DEBUG',
                    'propagate': True,
                },
                'models': {
                    'handlers': ['models_debug', 'models_warning'],
                    'level': 'DEBUG',
                    'propagate': True,
                },
                'utils': {
                    'handlers': ['utils_debug', 'utils_warning'],
                    'level': 'DEBUG',
                    'propagate': True,
                },
            },
        }
